#include "imlogindlg.h"
#include "ui_imlogindlg.h"
#include "../Biz/WWCommonUtility.h"
#include "../Core/xparam.hpp"
#include "../FrontEnd/utility_inline.hpp"
#include <qevent.h>
#include <QMessageBox>

using namespace NetMsgBus;
using namespace wwcommon;
enum IMPredefinedStatus_T
{
IMPREDEFSTATUS_OFFLINE = 0 , // 离线
IMPREDEFSTATUS_FREE	 , // 我有空
IMPREDEFSTATUS_BUSY	 , // 忙碌
IMPREDEFSTATUS_AWAY	 , // 离开
IMPREDEFSTATUS_INCALL , // 电话中
IMPREDEFSTATUS_OUTFORDINNER	, // 就餐
IMPREDEFSTATUS_WAIT	, // 稍候
IMPREDEFSTATUS_INVISIBLE	, // 隐身
IMPREDEFSTATUS_OFFLINELOGON , // 离线登陆状态
};


IMLoginDlg::IMLoginDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::IMLoginDlg),
    m_shellStartLoginName("")
{
    ui->setupUi(this);
    QObject::connect(ui->login_btn, SIGNAL(clicked()), SLOT(OnLoginBtn()));
    QObject::connect(this, SIGNAL(initloginList()), SLOT(OnInitLoginList()));
    emit initloginList();

}

IMLoginDlg::~IMLoginDlg()
{
    delete ui;
}

void IMLoginDlg::OnInitLoginList()
{
    core::XParam xp;
    xp.FromJsonFile(FileUtility::MakeWWProfileRoot() + "/recentlogin.json");
    std::vector<core::XParam> loginlist;
    xp.get_XParamVector("loginlist", loginlist);
    for(size_t i=0; i < loginlist.size(); i++)
    {
        string loginuid;
        loginlist[i].get_Str("uid", loginuid);
        bool islastlogin = false;
        loginlist[i].get_Bool("islast", islastlogin);
        m_loginlist.insert(QString::fromStdString(loginuid));
        ui->username_edit_comb->addItem(QString::fromStdString(loginuid));
        if(islastlogin)
            ui->username_edit_comb->setEditText(QString::fromStdString(loginuid));
    }
}

void IMLoginDlg::showEvent(QShowEvent * event)
{
    if(m_shellStartLoginName.size() > 0)
    {
        ui->username_edit_comb->setEditText(m_shellStartLoginName);
        m_shellStartLoginName.clear();
    }
    emit LoginShowed();
}

void IMLoginDlg::setLoginName(const std::string &uid)
{
    m_shellStartLoginName = QString::fromStdString(uid);
}

void IMLoginDlg::setLoginErrInfo(const QString &errinfo)
{
    ui->login_errinfo->setText(errinfo);
}

void IMLoginDlg::OnLoginFailed(QString errinfo)
{
    setLoginErrInfo(errinfo);
    ui->login_btn->setEnabled(true);
}

void IMLoginDlg::OnLoginOK(MsgBusParam)
{
    ui->login_btn->setEnabled(true);
    setLoginErrInfo("login success.");
    done(QDialog::Accepted);
    QString uid = ui->username_edit_comb->currentText();
    if(ui->username_edit_comb->findText(uid) == -1)
        ui->username_edit_comb->addItem(uid);

    if(ui->remembercheckBox->checkState() == Qt::Checked)
        m_loginlist.insert(uid);
    else
        m_loginlist.erase(uid);

    std::vector<core::XParam> loginlist;

    std::set<QString>::const_iterator cit = m_loginlist.begin();
    while(cit != m_loginlist.end())
    {
        core::XParam onelogin;
        onelogin.put_Str("uid", (*cit).toStdString());
        if(uid.compare(*cit) == 0)
            onelogin.put_Bool("islast", true);
        else
            onelogin.put_Bool("islast", false);
        loginlist.push_back(onelogin);
        ++cit;
    }
    core::XParam xp;
    xp.put_XParamVector("loginlist", loginlist);
    xp.ToJsonFile(FileUtility::MakeWWProfileRoot() + "/recentlogin.json");
}

void IMLoginDlg::OnLoginBtn()
{
    std::string userid;
    std::string pass;
    userid = ui->username_edit_comb->currentText().toStdString();
    pass = ui->password_edit->text().toStdString();
    QString loginstateText = ui->login_state_comboBox->currentText();
    //QMessageBox::information(NULL, "test", loginstateText);
    if(userid.empty())
    {
        setLoginErrInfo("please input user name");
        return;
    }
    if(pass.empty())
    {
        setLoginErrInfo("please input password");
        return;
    }
    int loginstate = IMPREDEFSTATUS_FREE;
    loginstateText = loginstateText.trimmed();
    if(loginstateText.compare("Free", Qt::CaseInsensitive) == 0)
    {
        loginstate = IMPREDEFSTATUS_FREE;
    }
    else if(loginstateText.compare("Busy", Qt::CaseInsensitive) == 0)
    {
        loginstate = IMPREDEFSTATUS_BUSY;
    }
    else if(loginstateText.compare("Away", Qt::CaseInsensitive) == 0)
    {
        loginstate = IMPREDEFSTATUS_AWAY;
    }
    else
    {

        loginstate = IMPREDEFSTATUS_INVISIBLE;
    }
//    IMPREDEFSTATUS_FREE	= 1,
//    IMPREDEFSTATUS_BUSY	= 2,
//    IMPREDEFSTATUS_AWAY	= 3,
//    IMPREDEFSTATUS_INCALL	= 4,
//    IMPREDEFSTATUS_OUTFORDINNER	= 5,
//    IMPREDEFSTATUS_WAIT	= 6,
//    IMPREDEFSTATUS_INVISIBLE	= 7,

    ui->password_edit->clear();
    setLoginErrInfo("login....");

    FrontEndUtilityInline::SimpleEncode(pass, pass, userid);
    emit DoLogin(userid, pass, loginstate);
    pass.clear();
    ui->login_btn->setEnabled(false);
}

void IMLoginDlg::on_password_edit_textChanged(const QString &arg1)
{
    ;
}
